/*
 * This file is part of LiquidBounce (https://github.com/CCBlueX/LiquidBounce)
 *
 * Copyright (c) 2015 - 2025 CCBlueX
 *
 * LiquidBounce is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * LiquidBounce is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with LiquidBounce. If not, see <https://www.gnu.org/licenses/>.
 */
package net.ccbluex.liquidbounce.features.item

import net.ccbluex.liquidbounce.LiquidBounce
import net.ccbluex.liquidbounce.utils.client.PlainText
import net.ccbluex.liquidbounce.utils.client.asPlainText
import net.ccbluex.liquidbounce.utils.client.mc
import net.minecraft.component.ComponentChanges
import net.minecraft.component.DataComponentTypes
import net.minecraft.component.type.ContainerComponent
import net.minecraft.component.type.LoreComponent
import net.minecraft.inventory.SimpleInventory
import net.minecraft.text.Text
import net.minecraft.util.Formatting

/**
 * Generates a list of [ComponentChanges]s from a [SimpleInventory]
 *
 * These [ComponentChanges]s can be used to recreate the inventory
 * using Minecraft Chests
 * by placing them together
 *
 * For example by typing this command:
 * give @p chest{display:{Name:"\"Hello!\""},BlockEntityTag:{Items:[{Slot:0,id:acacia_boat,Count:1}]}} 1
 *
 * This will give you a chest with the name "Hello!" and an acacia boat in the first slot
 * However, this will only work for the first 27 slots and not for the rest. This is why we split the
 *  inventory into multiple [ComponentChanges]s
 *
 * There should also be [net.ccbluex.liquidbounce.features.command.commands.client.CommandContainers]
 * `.containers add <nbt>` which will give you the chest with the given nbt
 *
 * @return A list of [ComponentChanges]s representing the inventory
 */
fun SimpleInventory.inventoryAsComponents(title: Text): List<ComponentChanges> {
    val stacks = heldStacks

    // If the inventory is empty, we don't need to do anything
    if (stacks.isEmpty()) {
        return emptyList()
    }

    return stacks.chunked(27).mapIndexed { containerIndex, stacks ->
        val builder = ComponentChanges.builder()

        builder.add(DataComponentTypes.CUSTOM_NAME, title)
        builder.add(
            DataComponentTypes.LORE, LoreComponent(
                listOfNotNull(
                    "Container #$containerIndex".asPlainText(Formatting.GOLD),
                    mc.currentServerEntry?.let {
                        "Server: ${it.address}".asPlainText(Formatting.GOLD)
                    },
                    PlainText.EMPTY,
                    "Generated by ${LiquidBounce.CLIENT_NAME} ${LiquidBounce.clientVersion}"
                        .asPlainText(Formatting.DARK_AQUA),
                    "Made with <3 by ${LiquidBounce.CLIENT_AUTHOR}"
                        .asPlainText(Formatting.YELLOW),
                )
            )
        )
        builder.add(DataComponentTypes.CONTAINER, ContainerComponent.fromStacks(stacks))

        builder.build()
    }
}

